import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
    {
        path: "/",
        redirect: "/dashboard",
    },
    {
        path: "/login",
        component: () =>
            import(/* webpackChunkName: "about" */ "../views/Login.vue"),
    },
    {
        path: "/register",
        component: () =>
            import(/* webpackChunkName: "about" */ "../views/Register.vue"),
    },
    {
        path: "/dashboard",
        component: () =>
            import(/* webpackChunkName: "about" */ "../views/Dashboard.vue"),
    },
];

const router = new VueRouter({
    mode: "history",
    base: process.env.BASE_URL,
    routes,
});

const whitePath = ["/login", "/register"];

// 路由守卫
router.beforeEach((to, from, next) => {
    // 检查是否存在登录状态（用户数据）
    const user = window.sessionStorage.getItem("user");
    // 判断是否存在该用户信息
    if (user || whitePath.includes(to.path)) {
        next();
    } else {
        next("/login");
    }
});

export default router;
